package com.ruoyi.web.controller.card;

import java.lang.annotation.ElementType;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.ruoyi.system.domain.GameType;
import com.ruoyi.system.service.GameTypeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.card.domain.Card;
import com.ruoyi.card.service.ICardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员卡管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-19
 */
@RestController
@RequestMapping("/card")
public class CardController extends BaseController
{
    @Autowired
    private ICardService cardService;

    @Autowired
    private GameTypeService gameTypeService;

    /**
     * 查询会员卡管理列表
     */
    @PreAuthorize("@ss.hasPermi('card:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(Card card)
    {
        startPage();
        List<Card> list = cardService.selectCardList(card);
        return getDataTable(list);
    }

    /**
     * 导出会员卡管理列表
     */
    @PreAuthorize("@ss.hasPermi('card:card:export')")
    @Log(title = "会员卡管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Card card)
    {
        List<Card> list = cardService.selectCardList(card);
        ExcelUtil<Card> util = new ExcelUtil<Card>(Card.class);
        util.exportExcel(response, list, "会员卡管理数据");
    }

    /**
     * 获取会员卡管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('card:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cardService.selectCardById(id));
    }

    /**
     * 新增会员卡管理
     */
    @PreAuthorize("@ss.hasPermi('card:card:add')")
    @Log(title = "会员卡管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Card card)
    {
        return toAjax(cardService.insertCard(card));
    }

    /**
     * 修改会员卡管理
     */
    @PreAuthorize("@ss.hasPermi('card:card:edit')")
    @Log(title = "会员卡管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Card card)
    {
        return toAjax(cardService.updateCard(card));
    }

    /**
     * 删除会员卡管理
     */
    @PreAuthorize("@ss.hasPermi('card:card:remove')")
    @Log(title = "会员卡管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        System.out.println("开始删除");
        System.out.println(ids);
        return toAjax(cardService.deleteCardByIds(ids));
    }
//获取娱乐类型
// 获取娱乐类型列表（分页）
@PreAuthorize("@ss.hasPermi('card:card:list')")
@GetMapping("/getEntertainmentNameList")
public TableDataInfo getEntertainmentNameList(
        @RequestParam String name)
{
    GameType gameType = gameTypeService.selectGameTypeByName(name);
    Long id = gameType.getId();
    Card card = new Card();
    card.setgameTypeId(id);
    // 2. 分页查询娱乐类型
    startPage();
    List<Card> list = cardService.selectCardList(card);
    // 3. 返回分页数据
    return getDataTable(list);
}
//会员卡更新
    @PutMapping("/updateCard")
    public AjaxResult updateCard(@RequestBody Card card){
        Long id = card.getId();
        Card card1 = cardService.selectCardById(id);
        System.out.println(card1);
        if(card1.getType()==2){
            card1.setCount(card1.getCount()-1);
            return toAjax(cardService.updateCard(card1));
        }
        return toAjax(cardService.updateCard(card1));
    }
//    获取当前类的会员卡
    @GetMapping("getCardList/{gameTypeId}")
    public TableDataInfo getCardList(@PathVariable Long gameTypeId){
        Card card = new Card();
        card.setgameTypeId(gameTypeId);

        return getDataTable(  cardService.selectCardList(card));


    }
}

